package com.android.tea.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.R;
import com.android.tea.entity.TeaCompany;
import com.android.tea.image.ImageLoaderManager;
import com.android.tea.shopper.ShopperDetailsActivity;
import com.android.tea.util.StringUtils;
import com.android.tea.util.TeaLog;
import com.android.tea.util.Utils;

public class ShoperAdapter extends BaseAdapter {

	private static final String TAG = "Adapter";
	private LayoutInflater inflater;
	private ArrayList<TeaCompany> list;
	private Context context;
	private ImageLoaderManager imageLoaderManager;

	public ShoperAdapter(Context context, ArrayList<TeaCompany> list) {
		super();
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoaderManager = new ImageLoaderManager(context, new Handler(),
				this);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public TeaCompany getItem(int position) {
		if (getCount() >= position + 1) {
			return list.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TeaCompany shopperMember = list.get(position);
		if (shopperMember == null) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.good_shooper_item, null);
			holder = new ViewHolder();
			holder.item = (RelativeLayout) convertView
					.findViewById(R.id.shopperItem);
			holder.ivAvatar = (ImageView) convertView
					.findViewById(R.id.shopperItemImage);
			holder.tvTitle = (TextView) convertView
					.findViewById(R.id.shopperName);
			holder.tvPhone = (TextView) convertView
					.findViewById(R.id.phoneNumber);
			holder.tvProduct = (TextView) convertView
					.findViewById(R.id.shopperPruduct);
			holder.tvRegion = (TextView) convertView
					.findViewById(R.id.shopperLocal);
			holder.tvSetEmail = (TextView) convertView
					.findViewById(R.id.shopperEmail);
			holder.tvAvageCost = (TextView) convertView
					.findViewById(R.id.shopperAverageCost);
			holder.id = position;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				TeaLog.e("click", "打开某条目");
				Intent intent = new Intent();
				intent.setClass(context, ShopperDetailsActivity.class);
				intent.putExtra("id", shopperMember.getCompanyId());
				context.startActivity(intent);

			}

		};

		holder.item.setOnClickListener(clickListener);
		holder.ivAvatar.setOnClickListener(clickListener);

		setData(shopperMember, holder);
		return convertView;
	}

	private void setData(TeaCompany shopperMember, ViewHolder holder) {

		// setImage(imageLoaderManager.getImage(shopperMember.getAvatar(),
		// R.drawable.tea_team, size), R.drawable.tea_team);
		holder.ivAvatar.setImageBitmap(imageLoaderManager.getImage(
				shopperMember.getCompanyLogo(), R.drawable.hu, Utils
						.dipToPixels(context, 74)));
		setTitle(holder.tvTitle, shopperMember.getCompanyName());
		if (checkStringValue(shopperMember.getCompanyTel())) {
			holder.tvPhone.setVisibility(View.GONE);
		} else {
			holder.tvPhone.setVisibility(View.VISIBLE);
			setTitle(holder.tvPhone, "联系电话:" + shopperMember.getCompanyTel());
		}

		if (checkStringValue(shopperMember.getCoinName())) {
			holder.tvProduct.setVisibility(View.GONE);
		} else {
			holder.tvProduct.setVisibility(View.VISIBLE);
			setTitle(holder.tvProduct, "主营产品:" + shopperMember.getCoinName());
		}

		if (checkStringValue(shopperMember.getCompanyCityCode())) {
			holder.tvRegion.setVisibility(View.GONE);
		} else {
			holder.tvRegion.setVisibility(View.VISIBLE);
			setTitle(holder.tvRegion, "地区:"
					+ shopperMember.getCompanyCityCode());
		}

		if (checkStringValue(shopperMember.getCompanyEmail())) {
			holder.tvSetEmail.setVisibility(View.GONE);
		} else {
			holder.tvSetEmail.setVisibility(View.VISIBLE);
			setTitle(holder.tvSetEmail, "邮箱:" + shopperMember.getCompanyEmail());
		}

		if (checkStringValue(shopperMember.getCompanyCaco())) {
			holder.tvAvageCost.setVisibility(View.GONE);
		} else {
			holder.tvAvageCost.setVisibility(View.VISIBLE);
			setTitle(holder.tvAvageCost, "人均消费:"
					+ shopperMember.getCompanyCaco());
		}

	}

	private boolean checkStringValue(String shopperMember) {
		return null == shopperMember || shopperMember.trim().length() == 0
				|| shopperMember.trim().equals("null");
	}

	private void setTitle(TextView tvTitle, String title) {
		if (StringUtils.isNotNullOrEmpty(title)) {
			tvTitle.setText(title);
		} else {
			tvTitle.setText(context.getResources().getString(R.string.unknown));
		}
	}

	static class ViewHolder {
		int id;
		RelativeLayout item;
		ImageView ivAvatar;
		TextView tvTitle;
		TextView tvProduct;
		TextView tvSetEmail;
		TextView tvPhone;
		TextView tvRegion;
		TextView tvAvageCost;

	}

}
